package com.hu.gulimallproduct.project.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hu.gulimallproduct.project.entity.PmsAttrEntity;
import com.hu.gulimallproduct.project.entity.PmsCategoryEntity;
import com.hu.gulimallproduct.project.service.PmsAttrAttrgroupRelationService;
import com.hu.gulimallproduct.project.service.PmsAttrService;
import com.hu.gulimallproduct.project.service.PmsCategoryService;
import com.hu.gulimallproduct.project.vo.AttrGroupCatelogIdVO;
import com.hu.gulimallproduct.project.vo.AttrGroupRelationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hu.gulimallproduct.project.entity.PmsAttrGroupEntity;
import com.hu.gulimallproduct.project.service.PmsAttrGroupService;
import com.hu.gulimallcommon.utils.PageUtils;
import com.hu.gulimallcommon.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author hbt
 * @email 2188748205@qq.com
 * @date 2024-05-20 12:32:11
 */
@RestController
@RequestMapping("product/attrgroup")
public class PmsAttrGroupController {
    @Autowired
    private PmsAttrGroupService pmsAttrGroupService;
    @Autowired
    PmsAttrAttrgroupRelationService groupRelationService;
    @Autowired
    private PmsCategoryService pmsCategoryService;
    @Autowired
    PmsAttrService pmsAttrService;
    @GetMapping("{attrGroupId}/attr/relation")
    public R relationAttr(@PathVariable Long attrGroupId){
        List<PmsAttrEntity> catelogPath = pmsAttrService.listRelationAttr(attrGroupId);
        return R.ok().put("data",catelogPath);
    }

    /**
     * 规格参数
     * @param catelogId
     * @return
     */
    @GetMapping("{catelogId}/withattr")
    public R withattrByCategoryId(@PathVariable Long catelogId){
        List<AttrGroupCatelogIdVO> catelogPath = pmsAttrGroupService.withattrByCategoryId(catelogId);
        return R.ok().put("data",catelogPath);
    }

    /**
     * 保存属性关联关系
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelationAttr(@RequestBody List<AttrGroupRelationVO> vos){
       groupRelationService.addRelationAttr(vos);
        return R.ok();
    }
    /**
     * 属性分组当前分组乜有关联的属性
     * @param attrGroupId
     * @return
     */
    @GetMapping("{attrGroupId}/noattr/relation")
    public R relationNoAttr(@PathVariable Long attrGroupId, @RequestParam Map<String, Object> params){
        PageUtils catelogPath = pmsAttrService.pageRelationNoAttr(attrGroupId,params);
        return R.ok().put("page",catelogPath);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
        PageUtils page = pmsAttrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		PmsAttrGroupEntity pmsAttrGroup = pmsAttrGroupService.getById(attrGroupId);
        Long catelogId = pmsAttrGroup.getCatelogId();
        pmsAttrGroup.setCatelogPath(pmsCategoryService.selectCatelogPath(catelogId));

        return R.ok().put("pmsAttrGroup", pmsAttrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PmsAttrGroupEntity pmsAttrGroup){
		pmsAttrGroupService.save(pmsAttrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PmsAttrGroupEntity pmsAttrGroup){
		pmsAttrGroupService.updateById(pmsAttrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		pmsAttrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
    /**
     * 删除属性关联关系
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVO[] groupRelationVO){
        pmsAttrGroupService.deleteRelation(groupRelationVO);
        return R.ok();
    }

}
